package com.example.activity

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.provider.MediaStore
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.EditText
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts

const val FIRST_MSG="FIRST_MSG"
const val SECOND_MSG="SECOND_MSG"
class MainActivity : AppCompatActivity() {

    var seconds=0
    var running=false
    var wasRunning=false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        Log.d("life cycle","onCreat")
        val button_start=findViewById<Button>(R.id.button_start)
        val button_stop=findViewById<Button>(R.id.button_stop)
        val button_restart=findViewById<Button>(R.id.button_restart)
        runTimer()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val first = findViewById<Button>(R.id.first)
        val editTextText = findViewById<EditText>(R.id.editTextText)
        first.setOnClickListener{
            val intent=Intent(this,SecondActivity::class.java)
            intent.putExtra(FIRST_MSG,editTextText.text.toString())
            startActivity(intent)
        }
        val second=findViewById<Button>(R.id.second)
        second.setOnClickListener {
            val intent=Intent(this,ThridActivity::class.java)
            startActivityForResult(intent,0)
        }

        if (savedInstanceState!=null){
            seconds=savedInstanceState.getInt("seconds")
            running=savedInstanceState.getBoolean("running")
            wasRunning=savedInstanceState.getBoolean("wasRunning")
        }
        button_start.setOnClickListener {
            running=true
        }
        button_stop.setOnClickListener {
            running=false
        }
        button_restart.setOnClickListener {
            running=true
            seconds=0
        }
    }
    fun runTimer(){
        val handler=Handler()
        val runnable=object :Runnable{
            override fun run(){
                val hours=seconds/3600
                val minutes=seconds/60
                val secs=seconds%60
                val textView_timer=findViewById<TextView>(R.id.textView_timer)
                textView_timer.text=String.format("%02d:%02d:%02d",hours,minutes,secs)
                if (running){
                    seconds++
                }
                handler.postDelayed(this,1000)
            }
        }
        handler.post(runnable)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("life cycle","onsaveInstanceState")
        outState.putInt("seconds",seconds)
        outState.putBoolean("running",running)
        outState.putBoolean("wasRunning",wasRunning)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("life cycle","onDestory")
    }
    override fun onStart() {
        super.onStart()
        Log.d("life cycle","onStart")
    }
    override fun onStop() {
        super.onStop()
        Log.d("life cycle","onStop")
    }
    override fun onResume() {
        super.onResume()
        Log.d("life cycle","onResume")
        if (wasRunning){
            running=true
        }
    }
    override fun onPause() {
        super.onPause()
        Log.d("life cycle","onPause")
        wasRunning=running
        running=false
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val imageView=findViewById<ImageView>(R.id.imageView)
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as Bitmap
            imageView.setImageBitmap(imageBitmap)
        }

        val textView=findViewById<TextView>(R.id.textView)
        if (requestCode==0){
            if (resultCode==1){
                textView.text=data?.getStringExtra(SECOND_MSG)
            }
        }
        val third=findViewById<Button>(R.id.thrid)
        third.setOnClickListener {
            dispatchTakePictureIntent()
        }
    }
    val REQUEST_IMAGE_CAPTURE=1
    private fun dispatchTakePictureIntent(){
        val takePictureIntent=Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (intent.resolveActivity(packageManager)!=null){
            startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE)
        }
    }
}